package com.itany.mall.service.proxy;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import com.itany.mall.entity.Order;
import com.itany.mall.entity.OrderDetail;
import com.itany.mall.exception.OrderNotFoundException;
import com.itany.mall.exception.ServiceException;
import com.itany.mall.factory.Autowired;
import com.itany.mall.factory.Component;
import com.itany.mall.jdbc.transaction.TransactionManager;
import com.itany.mall.service.OrderService;

@Component("orderServiceProxy")
public class OrderServiceProxy implements OrderService {
	@Autowired
	private TransactionManager tm;

	@Autowired("orderServiceImpl")
	private OrderService impl;

	@Override
	public void commitOrder(Collection<OrderDetail> shopingCart) throws ServiceException {
		try {
			tm.beginTransaction();
			impl.commitOrder(shopingCart);
			tm.commit();
		} catch (Exception e) {
			tm.rollback();
			e.printStackTrace();
			throw new ServiceException();
		}
	}

	@Override
	public List<Order> findMyOrders(long userId) throws ServiceException {
		try {
			tm.beginTransaction();
			List<Order> list = impl.findMyOrders(userId);
			tm.commit();
			return list;
		} catch (Exception e) {
			tm.rollback();
			e.printStackTrace();
			throw new ServiceException();
		}
	}

	@Override
	public Map<Order, List<OrderDetail>> findDetailsByOrderNum(String num, long userId)
			throws OrderNotFoundException, ServiceException {
		try {
			tm.beginTransaction();
			Map<Order, List<OrderDetail>> map = impl.findDetailsByOrderNum(num, userId);
			tm.commit();
			return map;
		} catch (OrderNotFoundException e) {
			tm.rollback();
			throw e;
		} catch (Exception e) {
			tm.rollback();
			e.printStackTrace();
			throw new ServiceException();
		}
	}

}
